package com.ac.utils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnalyzeUtils {

    public static String replace(String message) {
        // 匹配${} 的表达式
        String pattern = "\\$\\{[^}]*\\}";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(message);
        // 当字符串中有匹配到 ${} 时
        while (m.find()) {
            // {} 和里面的内容
            String param = m.group(0);
            // 输出${} 里面的内容
//            System.out.println(param.substring(2, param.length() - 1));
            // 替换 ${}里面的内容
            message = message.replaceFirst(param.substring(2, param.length() - 1), "");
        }
        return message;
    }

    public static String replaceParams(String statement, List<String> params) {
        String regex = "\\$\\{\\}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(statement);

        StringBuilder result = new StringBuilder();
        int lastIndex = 0;

        for (String param : params) {
            if (matcher.find()) {
                result.append(statement, lastIndex, matcher.start()).append(param);
                lastIndex = matcher.end();
            } else {
                break;
            }
        }

        result.append(statement.substring(lastIndex));
        System.out.println(result.toString());
        return result.toString();
    }
}
